package com.application.Activity;

import java.util.ArrayList;

import ReadWriteXML.CMember;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.DTO.AccountDTO;
import com.application.DTO.RoleDTO;
import com.application.PhanQuyen.Quyen;

 
public class frmTaoNhom extends Activity {
	public static int ON_EXIT_APP=1111;
	public static String CODE_GROUP_NAME="CODE_GROUP_NAME";
	int port=5033;
	Button btnTaoNhom;
	Button btnCaiDatVaiTro;
	EditText txtTenNhom;
	EditText txtSoLuongThanhVien;
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_tao_nhom);
		Log.i("Phu", "on create tao nhom");
		
		btnTaoNhom=(Button)findViewById(R.id.btnTaoNhom);
		btnCaiDatVaiTro=(Button)findViewById(R.id.btnCaiDatVaiTro);
		txtTenNhom=(EditText)findViewById(R.id.txtTenNhom);
		txtSoLuongThanhVien=(EditText)findViewById(R.id.txtSoLuongThanhVien);
		
		btnTaoNhom.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {			
				//Tạo file danh sách thành viên				
				createMemberFile();				
				NetworkController._peer.account.isThanhLapNhom = true;
				Intent i = new Intent(getBaseContext(), frmNhomChoActivity.class);
				i.putExtra(CODE_GROUP_NAME, txtTenNhom.getText().toString());
				NetworkController.setGroupName(String.valueOf(txtTenNhom.getText()));
				
				//ghi xuống file Log
				String text = "Đã tạo nhóm.." + txtTenNhom.getText();
		        //WriteToLogFile(text);
				ReadWriteFileController.checkRuleWriteToLogFile(text);
				startActivity(i);
			}
		});
		
		btnCaiDatVaiTro.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {		

				Intent i=new Intent(getBaseContext(),frmQuanLyRoleActivity.class);
				
				//ghi xuống file Log
				String text = "Đã vào Quản lý Role..";
		        //WriteToLogFile(text);
				ReadWriteFileController.checkRuleWriteToLogFile(text);
		        
				startActivity(i);
			}
		});
	}
	private void createMemberFile(){
		CMember member=new CMember();
		member.set_fullName(NetworkController._peer.account.get_Username());
		member.set_ip(NetworkController._peer.getAddressPeer());
		member.set_userName(NetworkController._peer.account.get_Username());		
		ReadWriteFileController.deleteFile(ReadWriteFileController.MEMBER_PATH);		
		ReadWriteFileController.insertMember(ReadWriteFileController.MEMBER_PATH, member);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		Log.d("PEERDROID", " vao on key down");
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(R.drawable.img_dialog);
			builder.setMessage("Bạn có chắc muốn thoát khỏi chương trình ?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int id) {
					if(!NetworkController.isOffline())
					{
						String s = NetworkController.get_ipHost();
						NetworkController.SendMessageLogout();
//						NetworkController.peerRelease();
						//xoá danh sách hàng xóm khi thoát khỏi ứng dụng
//						ReadWriteFileController.deleteFile(ReadWriteFileController.pathNeighborsList);
						
					}
					//NetworkController.SendMessageLogout(new Address("192.168.173.20:5033"));
					Log.d("PEERDROID","Đã Thoát khỏi chương trình");
					finish();
//					System.exit(0);
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 255, 255, 255))); 
			alert.show();
			return true;
		}
		return super.onKeyDown(keyCode, event);	
	}
	
	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
		Log.i("Phu", "on start tao nhom");
	//	Log.i("Phu", "account: "+NetworkController._peer.getAccount().get_Username());
		if (NetworkController._peer == null) {
			Log.i("Phu", "peer "+(NetworkController._peer==null));
			AccountDTO account = new AccountDTO();
			NetworkController.init("MES", port, account);	
		}
		ArrayList<Quyen> ruleList=ReadWriteFileController.readRule(getApplicationContext(), ReadWriteFileController.RULE_BASE_PATH);
		Log.i("Phu", "rule list "+(ruleList==null));
		RoleDTO roleSangLapNhom=new RoleDTO("Người sáng lập nhóm", ruleList);		
		Log.i("Phu", "set role sang lap nhom");
		NetworkController._peer.getAccount().addRole(roleSangLapNhom);
		Log.i("Phu", "add role");
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("Phu","on onActivityResult nhom cho");
		 if (resultCode == ON_EXIT_APP) {
			 Intent i=new Intent(this, FrmTabActivity.class);
			// startActivityForResult(i, ON_EXIT_APP);
			 setResult(ON_EXIT_APP);
			 finish();
		 }
	}
	//An
	/*public void WriteToLogFile(String text){
		Log.d("Phu", NetworkController._peer.getAccount().get_Username() + "-"+ text );
        String username = NetworkController._peer.getAccount().get_Username();
		Log.d("Phu", "ten nhom 2....: "+ txtTenNhom.getText());
		String path = ReadWriteFileController.DIRECTORY + txtTenNhom.getText()+"LogList.xml";
		Log.d("Phu", "Path: "+ path);
        ReadWriteFileController.insertLogFile(path, text, username);
	}*/

}
